package com.windfone.web.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.common.StringUtil;
import com.windfone.model.Member;
import com.windfone.model.MemberDetail;
import com.windfone.service.MemberService;

public class MemberAction extends ActionSupport {

	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private MemberService memberService;
	private String name;
	private String cardId;
	private int level;
	private int points;
	private int status;
	private  Date createTime;
	private Member memberBean;
	
	
	private int memberId;
	private int sex;
	private Date birthday;
	private String birthdayStr;
	private int ageRange;
	private String headerImage;
	private String email;
	private String mobileNumber;
	private String personId;
	public String getBirthdayStr() {
		return birthdayStr;
	}
	public void setBirthdayStr(String birthdayStr) {
		this.birthdayStr = birthdayStr;
	}

	private int memberDetailId;
	private MemberDetail memberDetail;
	private String password;
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public String getCardId() {
		return cardId;
	}

	public int getLevel() {
		return level;
	}

	public int getPoints() {
		return points;
	}

	public int getStatus() {
		return status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public Member getMemberBean() {
		return memberBean;
	}

	public int getMemberId() {
		return memberId;
	}

	public int getSex() {
		return sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public int getAgeRange() {
		return ageRange;
	}

	public String getHeaderImage() {
		return headerImage;
	}

	public String getEmail() {
		return email;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public String getPersonId() {
		return personId;
	}

	public int getMemberDetailId() {
		return memberDetailId;
	}

	public MemberDetail getMemberDetail() {
		return memberDetail;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setMemberBean(Member memberBean) {
		this.memberBean = memberBean;
	}

	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setAgeRange(int ageRange) {
		this.ageRange = ageRange;
	}

	public void setHeaderImage(String headerImage) {
		this.headerImage = headerImage;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}

	public void setMemberDetailId(int memberDetailId) {
		this.memberDetailId = memberDetailId;
	}

	public void setMemberDetail(MemberDetail memberDetail) {
		this.memberDetail = memberDetail;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String checkLoginName() throws Exception {
		Member m = new Member();
		m.setLoginName(loginName);
		existFlag = memberService.checkMember( m );
		
		return SUCCESS;
	}
	public String memberList() throws Exception {
		String whereCondition = "";
		if ( level !=-1 ) {
			whereCondition = whereCondition+" AND level = "+level;
		}
		if ( name !=null && !name.equals("") ) {
			whereCondition =whereCondition+ " AND name = '"+name +"' ";
		}
		if ( cardId !=null && !cardId.equals("") ) {
			whereCondition = whereCondition + " AND cardId = '"+cardId +"' ";
		}
		
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = memberService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		members = memberService.getMembers(beginRecord, whereCondition);
		return SUCCESS;
	}
	
	public String beforeUpdateMember() throws Exception {
		memberBean = memberService.getMemberByKey( memberId );
		return SUCCESS;
	}
	public String loadMemberDetail() throws Exception {
		memberDetail = memberService.getMemberDetailByKey(memberId );
		if ( memberDetail == null ) {
			memberDetail = new MemberDetail();
			memberDetail.setId(-1);
			memberDetail.setSex(-1);
			memberDetail.setMemberId(memberId);
			memberDetail.setAgeRange(-1);
		}
		return SUCCESS;
	}
	public String updateMember() throws Exception {
		Member bean = new Member();
		bean.setCardId(cardId);
		bean.setLevel(level);
		bean.setName(name);
		bean.setPoints(points);
		bean.setStatus(status);
		bean.setPassword(   password );
		bean.setId(memberId);
		memberService.updateMember(bean);
		level = -1;
		name = "";
		cardId = "";		
		return SUCCESS;
	}
	
	public MemberService getMemberService() {
		return memberService;
	}
	public List<Member> getMembers() {
		return members;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public void setMembers(List<Member> members) {
		this.members = members;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	private List<Member> members;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private String loginName;
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	private boolean existFlag = false;
 
	public boolean isExistFlag() {
		return existFlag;
	}
	public void setExistFlag(boolean existFlag) {
		this.existFlag = existFlag;
	}
	

	public String addMember() throws Exception {
		Member bean = new Member();
		bean.setCardId(cardId);
		bean.setLevel(level);
		bean.setName(name);
		bean.setPoints(points);
		bean.setStatus(status);
		bean.setPassword(  cardId  );
		memberService.addMember(bean);
		level = -1;
		name = "";
		cardId = "";
		return SUCCESS;
	}
	
	public String deleteMember() throws Exception {
		memberService.deleteMember(memberId);
		memberService.deleteMemberDetailByMemeberId(memberId);
		return SUCCESS;
	}
	
 
	public String editMemberDetail() throws Exception {
		memberDetail = new MemberDetail();
		memberDetail.setAgeRange(ageRange);
		memberDetail.setMemberId(memberId);
		memberDetail.setId(memberDetailId);
		if ( birthdayStr != null && !birthdayStr.equals("")) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			memberDetail.setBirthday( format.parse(birthdayStr) );
		}
		memberDetail.setEmail(email);
		memberDetail.setHeaderImage(headerImage);
		memberDetail.setPersonId(personId);
		memberDetail.setMobileNumber(mobileNumber);
		memberDetail.setSex(sex);
		if ( memberDetailId == -1 ){
			memberService.addMemberDetail(memberDetail);
		} else {
		memberService.updateMemberDetail(memberDetail);
		}
		return SUCCESS;
	}

	
}
